package com.edu.vn;

import java.util.Random;
import java.util.concurrent.ExecutionException;


import android.app.Activity;
import android.content.ContentValues;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Contacts;
import android.util.Log;
import android.widget.TextView;

public class ContactsAdding extends Activity {
	String organisation[] = {"イ.ソフト","NTT","Docomo","日立","IBM","FPT","三菱","SANYO","Panasonic","Google"};
	String akasa[] = {"あ","か","さ","た","な"};
	Random random = new Random();
	private static final String LOG = ContactsAdding.class.getSimpleName();
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
    	super.onResume();
    	TextView tv = (TextView) findViewById(R.id.adding);
		tv.setText("連絡先を追加している、少少待ちください。");

		for(int akasaPos = 0; akasaPos <akasa.length; akasaPos++) {
		    for(int orgPos = 0; orgPos < 20; orgPos++) {
			    DownloadTask downloadTask = new DownloadTask();
			    downloadTask.execute(akasaPos, orgPos);
		    	try {
					downloadTask.get();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
						e.printStackTrace();
				}
		    }
		}

    }

//    public void insertDummyAccount() {
////    	String akasa[] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
////    	String alphabeta[] ={"あ","か","さ","た","な","は","ま","や","ら","わ"};
//
//
//    	/*日本語ユーザ登録*/
//
//    	for(int akasaPos = 0; akasaPos <akasa.length; akasaPos++) {
//    		for(int orgPos = 0; orgPos < 20; orgPos++) {
//	    	insertAccount( akasaPos, orgPos);
//    		}
//    	}
//    }

	private String insertAccount(int akasaPos, int orgPos) {
		// create a new name
		String NAME = akasa[akasaPos]+"- ダミーユーザ - "+orgPos;
		String ORG = organisation[random.nextInt(organisation.length)];
		ContentValues values = new ContentValues();
		values.put(Contacts.People.NAME, NAME);
		values.put(Contacts.People.PHONETIC_NAME, akasa[akasaPos]);
		// add it to the database
		Uri newPersonUri = Contacts.People.createPersonInMyContactsGroup (getContentResolver(), values);
		// add mobile phone number
		ContentValues mobileValues = new ContentValues();
		Uri mobileUri = Uri.withAppendedPath(newPersonUri,
				Contacts.People.Phones.CONTENT_DIRECTORY);
		mobileValues.put(Contacts.Phones.NUMBER,
				"(660) 111-1111");
		mobileValues.put(Contacts.Phones.TYPE,
				Contacts.Phones.TYPE_MOBILE);
		getContentResolver().insert(mobileUri, mobileValues);

		// add company (organisation)
		ContentValues organisationValues = new ContentValues();
		Uri orgUri = Uri.withAppendedPath(newPersonUri,
				Contacts.Organizations.CONTENT_DIRECTORY);
		organisationValues.put(Contacts.Organizations.COMPANY,
				ORG);
		organisationValues.put(Contacts.Organizations.TYPE,
				Contacts.Organizations.TYPE_WORK);
		getContentResolver().insert(orgUri, organisationValues);
		String contactName = NAME+"adding ---------------------";

		Log.d(LOG, contactName);
		return contactName;
	}

    private class DownloadTask extends AsyncTask<Integer, Integer, String> {

		@Override
		protected String doInBackground(Integer... params) {
			String contactName = insertAccount(params[0],params[1]);
			return contactName;
		}


		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			TextView tv = (TextView) findViewById(R.id.adding);
			tv.setText("連絡先を追加終了。");
		}

    }
}